package com.example.springbootredis.controller;


import com.example.springbootredis.entiy.ShippingAddress;
import com.example.springbootredis.service.ShippingAddressService;
import com.example.springbootredis.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("shipping-address")
@CrossOrigin(maxAge = 3600)
public class ShippingAddressController {

    @Autowired
    private ShippingAddressService shippingAddressService;

    private Long getMemberIdFromRequest(HttpServletRequest request) {
        // token解析出会员ID
        return 1L;
    }

    @PostMapping("save")
    public JsonResult save(@RequestBody ShippingAddress shippingAddress, HttpServletRequest request) {
        Long memberId = getMemberIdFromRequest(request);
        shippingAddress.setMemberId(String.valueOf(memberId));

        // 必填字段验证
        if (shippingAddress.getConsignee() == null || shippingAddress.getConsignee().isEmpty()) {
            return JsonResult.failed("收货人不能为空");
        }
        if (shippingAddress.getPhone() == null || shippingAddress.getPhone().isEmpty()) {
            return JsonResult.failed("手机号不能为空");
        }
        if (shippingAddress.getDetailAddress() == null || shippingAddress.getDetailAddress().isEmpty()) {
            return JsonResult.failed("详细地址不能为空");
        }

        ShippingAddress savedAddress = shippingAddressService.save(shippingAddress);
        return JsonResult.success("操作成功", savedAddress);
    }

    @GetMapping("list")
    public JsonResult list(HttpServletRequest request) {
        Long memberId = getMemberIdFromRequest(request);
        List<ShippingAddress> addressList = shippingAddressService.findByMemberId(memberId);
        return JsonResult.success("获取成功", addressList);
    }

    @GetMapping("detail")
    public JsonResult detail(@RequestParam Long id, HttpServletRequest request) {
        Long memberId = getMemberIdFromRequest(request);
        Optional<ShippingAddress> address = shippingAddressService.findByIdAndMemberId(id, memberId);
        if (address.isPresent()) {
            return JsonResult.success("获取成功", address.get());
        } else {
            return JsonResult.failed("地址不存在");
        }
    }

    @PostMapping("update")
    public JsonResult update(@RequestBody ShippingAddress shippingAddress, HttpServletRequest request) {
        Long memberId = getMemberIdFromRequest(request);
        shippingAddress.setMemberId(String.valueOf(memberId));

        ShippingAddress updatedAddress = shippingAddressService.save(shippingAddress);
        return JsonResult.success("更新成功", updatedAddress);
    }

    @GetMapping("delete")
    public JsonResult delete(@RequestParam Long id, HttpServletRequest request) {
        Long memberId = getMemberIdFromRequest(request);
        try {
            shippingAddressService.deleteByIdAndMemberId(id, memberId);
            return JsonResult.success("删除成功", null);
        } catch (RuntimeException e) {
            return JsonResult.failed(e.getMessage());
        }
    }

    @PostMapping("set-default")
    public JsonResult setDefault(@RequestParam Long id, HttpServletRequest request) {
        Long memberId = getMemberIdFromRequest(request);
        try {
            shippingAddressService.setDefaultAddress(id, memberId);
            return JsonResult.success("设置默认地址成功", null);
        } catch (RuntimeException e) {
            return JsonResult.failed(e.getMessage());
        }
    }
}